﻿/*
    File name:          I4ProViewModel
    File type:          Class
    Author:             Mads Skipper Pedersen 07299
    Creation date:      15-03-2011
    Last modified:      18-05-2011
    Last modified by:   Mads Skipper Pedersen 07299
    Description:        The main ViewModel for the project, here
                        it is managed which tabs are shown to the
                        user and status messages from the robot 
                        are shown in a statusbar.
*/

using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using AdminManageUsers;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using i4pro_gruppe_d.Types;
using TechLog;
using TechProgram;

namespace i4pro_gruppe_d.ViewModel
{
    public class ViewModel : ViewModelBase 
    {
        #region Members

        private Messenger MessengerInstance;

        private LogTab techLogTabInstance;
        private ProgramTab techProgramtabInstance;
        private OppStd.OppStd oppStdTabInstance;
        private ManageUsers userManagementTabInstance;

        private TabItem logTab;
        private TabItem programTab;
        private TabItem stdTab;
        private TabItem userManagementTab;

        private User loggedInUser;

        #endregion
        #region Propertys
        public ObservableCollection<TabItem> TechnicianTab
        {
            get;
            set;
        }
        public ObservableCollection<TabItem> OpperatorTab
        {
            get;
            set;
        }

        public ObservableCollection<TabItem> AdminTab
        {
            get;
            set;
        }

        private Visibility loginVisible;
        public Visibility LoginVisible
        {
            get
            {
                return loginVisible;
            }
            set
            {
                loginVisible = value;
                RaisePropertyChanged("LoginVisible");
            }
        }

        private Visibility technicianVisible;
        public Visibility TechnicianVisible
        {
           get
           {
               return technicianVisible;
           }
            set
            {
                technicianVisible = value;
                RaisePropertyChanged("TechnicianVisible");
            }
        }
        private Visibility opperatorVisible;
        public Visibility OpperatorVisible
        {
            get
            {
                return opperatorVisible;
            }
            set
            {
                opperatorVisible = value;
                RaisePropertyChanged("OpperatorVisible");
            }
        }

        private Visibility adminVisible;
        public Visibility AdminVisible
        {
            get
            {
                return adminVisible;
            }
            set
            {
                adminVisible = value;
                RaisePropertyChanged("AdminVisible");
            }
        }

        private string currentCommand;

        public string CurrentCommand
        {
            get
            {
                return currentCommand;
            }
            set
            {
                currentCommand = value;
                RaisePropertyChanged("CurrentCommand");
            }
        }
        #endregion
        #region Constructor
        public ViewModel()
        {
            MessengerInstance = Messenger.Default;
            MessengerInstance.Register<User>(this, success);
            MessengerInstance.Register<string>(this, "ExecutingCommand", CurrentCommandChanged);
            MessengerInstance.Register<bool>(this, "Homing", LoadingHoming);

            CurrentCommand = "Stopped!";

            TechnicianTab = new ObservableCollection<TabItem>();
            OpperatorTab = new ObservableCollection<TabItem>();
            AdminTab = new ObservableCollection<TabItem>();

            techLogTabInstance = new LogTab();
            techProgramtabInstance = new ProgramTab();
            oppStdTabInstance = new OppStd.OppStd();
            userManagementTabInstance = new ManageUsers();

            logTab = new TabItem() { Header = "System Log" };
            programTab = new TabItem() { Header = "Programmer" };
            stdTab = new TabItem() { Header = "Opperator View" };
            userManagementTab = new TabItem() {Header = "User Management"};

            logTab.Content = techLogTabInstance;
            programTab.Content = techProgramtabInstance;
            stdTab.Content = oppStdTabInstance;
            userManagementTab.Content = userManagementTabInstance;

            TechnicianTab.Add(logTab);
            TechnicianTab.Add(programTab);
            OpperatorTab.Add(stdTab);
            AdminTab.Add(userManagementTab);
            success(new User("","",-1));
        }
        #endregion
        #region Methods
        private void LoadingHoming(bool i)
        {
            if(i)
            {
                LoginVisible = Visibility.Hidden;
                TechnicianVisible = Visibility.Hidden;
                AdminVisible = Visibility.Hidden;
            }
            else
            {
                LoginVisible = Visibility.Visible;
                success(loggedInUser);
            }
        }
		#endregion
        #region Commands
        public void success(User user)
        {
            loggedInUser = user;
            switch (loggedInUser.UserType)
            {
                case -1: //Not Loggedd In
                    TechnicianVisible = Visibility.Hidden;
                    OpperatorVisible = Visibility.Hidden;
                    AdminVisible = Visibility.Hidden;
                    break;
                case 0: //Logged in as Opperator
                    OpperatorVisible = Visibility.Visible;
                    TechnicianVisible = Visibility.Hidden;
                    AdminVisible = Visibility.Hidden;
                    break;
                case 1: //Logged in as Technician
                    OpperatorVisible = Visibility.Visible;
                    TechnicianVisible = Visibility.Visible;
                    AdminVisible = Visibility.Hidden;
                    break;
                case 2: //Logged in as Admin
                    OpperatorVisible = Visibility.Visible;
                    TechnicianVisible = Visibility.Visible;
                    AdminVisible = Visibility.Visible;
                    break;
				default: //Error
					TechnicianVisible = Visibility.Hidden;
                    OpperatorVisible = Visibility.Hidden;
                    AdminVisible = Visibility.Hidden;
            		break;
            }
        }

        private void CurrentCommandChanged(string command)
        {
            CurrentCommand = command;
        }
        #endregion
    }
}
